import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/widget_utils.dart';

//文字滚动效果
class FontMarquee extends StatefulWidget {
  final List<String> list;
  FontMarquee({Key key, @required this.list}) : super(key: key);

  @override
  FontMarqueeState createState() => FontMarqueeState();
}

class FontMarqueeState extends State<FontMarquee> with WidgetsBindingObserver {
  GlobalKey _myKey = new GlobalKey();
  ScrollController _controller;
  int index = 0;
  List<String> _list = [];
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  void initState() {
    _list = this.widget.list;
    //来监听 节点是否build完成
    WidgetsBinding widgetsBinding = WidgetsBinding.instance;
    widgetsBinding.addPostFrameCallback((callback) {
      Timer.periodic(new Duration(seconds: 5), (timer) {
        index += _myKey.currentContext.size.height.toInt();
        _controller.animateTo((index).toDouble(), duration: new Duration(seconds: 2), curve: Curves.easeOutSine);
        //滚动到底部从头开始
        if ((index - _myKey.currentContext.size.height.toInt()).toDouble() > _controller.position.maxScrollExtent) {
          _controller.jumpTo(_controller.position.minScrollExtent);
          index = 0;
        }
      });
    });
    _controller = new ScrollController(initialScrollOffset: 0);
    /*   _controller.addListener(() {
      print(_controller.offset);
    });*/
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        key: _myKey,
        //禁止手动滑动
        physics: new NeverScrollableScrollPhysics(),
        itemCount: _list.length,
        //item固定高度
        itemExtent: 42,
        scrollDirection: Axis.vertical,
        controller: _controller,
        itemBuilder: buildItem);
  }

  Widget buildItem(BuildContext context, int index) {
    return GestureDetector(
      onTap: () {
        // if (!BaseConstant.isLogin) {
        //   RouteUtil.goView(PageConstance.LOGIN_PAGE);
        //   return;
        // }
        // RouteUtil.goParameter(
        //     context, DetailsPage(acuId: _listEntity[index].aucId));
      },
      child: Container(
        // margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: _xunchaItem(_list[index]),
      ),
    );
  }

  Widget _xunchaItem(String title) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      child: WidgetUtils.getTextColorView(title, Colours.text_2222, Dimens.FONT_SIZE_13),
    );
  }
}
